﻿package client;
import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;

import server.ComBuyInterface;
import server.ComHouseInterface;
import server.PersonInterface;

import client.Configuration;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

public class ComSetKeyGUI extends JDialog {
	
	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JButton button;
	Person p = new Person();
	int pid=p.getPID();

	/*public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ComSetKeyGUI frame = new ComSetKeyGUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	


	/**
	 * Create the frame.
	 */
	public ComSetKeyGUI() {
		

		setTitle("Buy House");
		setModalityType(ModalityType.APPLICATION_MODAL);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("\u8BBE\u7F6E\u94A5\u5319\uFF1A");
		lblNewLabel.setBounds(55, 111, 79, 31);
		contentPane.add(lblNewLabel);
		
		textField = new JTextField();
		textField.setBounds(144, 116, 126, 21);
		contentPane.add(textField);
		textField.setColumns(10);
		
		JButton btnNewButton = new JButton("\u786E\u5B9A");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String k = textField.getText();
				String hN = textField_1.getText();
				int hn = Integer.parseInt(hN);
				System.out.println(hn);
				System.out.println(k);
				System.out.println(pid);
				
				Configuration config = new Configuration();
				String rmiAddress=config.getRmiAddress();
				ComHouseInterface comHouse;
				ComBuyInterface comBuy;
					
					try {
						comHouse = (ComHouseInterface) Naming.lookup(rmiAddress+"ComHouseList");
						String i = comHouse.getKey(hn);
						String kind = comHouse.getKind(hn);
						System.out.print(kind);
						
						if(i == null){
						boolean w = comHouse.setKey(hn, k);
						boolean x = comHouse.setOwnID(hn, pid);
					    System.out.println(w);
					    System.out.println(x);
						
						comBuy = (ComBuyInterface) Naming.lookup(rmiAddress+"ComBuyList");
						int ha = comBuy.getHap(kind);
						double mon = comBuy.getMon(kind);
						System.out.println(ha);
					
						PersonInterface person = (PersonInterface) Naming.lookup(rmiAddress+"Person");
						boolean y = person.hapChanges(pid, ha,'+');
						boolean z = person.cashChanges(pid, mon, '-');
						if(y&& z)
						JOptionPane.showMessageDialog(null, "您成功买下了一座房子，增加"+ha+"点Hap,花费"+mon+"元");
						else
							JOptionPane.showMessageDialog(null, "您的余额不足。");
						
						ComHouseInforGUI chg = new ComHouseInforGUI();
						chg.setLocationRelativeTo(null);
						chg.setVisible(true);
						dispose();
						}
						
						else
							JOptionPane.showMessageDialog(null, "该房已卖出.");;
							
						
					} catch (MalformedURLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (RemoteException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (NotBoundException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} 
					
			}
		});
		btnNewButton.setBounds(160, 191, 93, 23);
		contentPane.add(btnNewButton);
		
		textField_1 = new JTextField();
		textField_1.setBounds(144, 48, 126, 21);
		contentPane.add(textField_1);
		textField_1.setColumns(10);
		
		JLabel label = new JLabel("\u9009\u62E9\u623F\u53F7\uFF1A");
		label.setBounds(55, 43, 79, 31);
		contentPane.add(label);
		
		button = new JButton("\u8FD4\u56DE");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ComHouseInforGUI chg = new ComHouseInforGUI();
				chg.setLocationRelativeTo(null);
				chg.setVisible(true);
				dispose();
			}
		});
		button.setBounds(343, 229, 62, 23);
		contentPane.add(button);
	}
}
